import React, { useEffect, useState } from 'react'
import { Table, Button, notification } from 'antd';
import { CheckOutlined, CloseOutlined, BulbFilled } from "@ant-design/icons"
import http from '../../../util/http'
import FormItemInput from 'antd/es/form/FormItemInput';
export default function Audit() {
    const [dataSource, setdataSource] = useState([]);
    const { username, roleId, region } = JSON.parse(localStorage.getItem("token"))
    useEffect(() => {
        const roleObj = {
            "1": "superadmin",
            "2": "admin",
            "3": "editor"
        }
        http.get(`/news?author=${username}&aduitState=1&_expand=category`).then(res => {
            const list = res.data;
            setdataSource(roleObj[roleId] === "superadmin" ? list : [
                ...list.filter(item => item.author === username),
                ...list.filter(item => item.region === region && roleObj[item.roleId] === "editor")
            ])
        }).catch(err => console.log(err))
    }, [username, roleId, region])
    console.log(dataSource)
    const columns = [{
        title: "新闻标题",
        dataIndex: "title",
        render: (title, item) => {
            return <a href={`/news-manage/preview/${item.id}`}>{title}</a>
        }
    }, {
        title: "作者",
        dataIndex: "author"
    }, {
        title: "新闻分类",
        dataIndex: "category",
        render: (categoryId) => {
            return categoryId.title
        }
    }, {
        title: "操作",
        render: (item) => {
            return <span>
                <Button type='primary' icon={<CheckOutlined />} style={{ marginRight: 10 }} shape="circle"
                    onClick={() => handlePublish(item)}></Button>
                <Button danger type="primary" icon={<CloseOutlined />} shape="circle"
                    onClick={() => handleRefuse(item)}
                ></Button>
            </span>
        }
    }]
    /* 点击通过审核 */
    const handlePublish = (item) => {
        setdataSource(dataSource.filter(data => data.id !== item.id));
        http.patch(`/news/${item.id}`, {
            auditState: 2,
            publishState: 1
        }).then(res => {
            notification.info({
                message: `通知`,
                description:
                    `您可以到【审核管理/审核管理】中查看你的新闻新闻状态`,
                placement: "bottomRight",
                style: {
                    color: '#33ff00'
                },
                icon: <BulbFilled style={{ color: '#ffff33' }} />
            });
        })
    }
    /* 点击取消审核 */
    const handleRefuse = (item) => {
        setdataSource(dataSource.filter(data => data.id !== item.id));
        http.patch(`/news/${item.id}`, {
            auditState: 3
        }).then(res => {
            notification.info({
                message: `通知`,
                description:
                    `您可以到【审核管理/审核管理】中查看你的新闻新闻状态`,
                placement: "bottomRight",
                style: {
                    color: '#33ff00'
                },
                icon: <BulbFilled style={{ color: '#ffff33' }} />
            });
        })
    }
    return (
        <div>
            <Table dataSource={dataSource} columns={columns}
                rowKey={item => item.id}
                pagination={{
                    pageSize: 7
                }}
            ></Table>
        </div>
    )
}
